Source: HighChart.js

(function () {
    "use strict";
    /**
     * Creates new HighChart
     * @class HighChart
     */
    var HighChart = function () {
        var helper = require("./TestHelper");
        var TestHelperPO = require("./TestHelperPO");
        var logger = require("./Logger.js");
        var fs = require("fs");
        var Promise = require("bluebird");

        var section, container, svg, tooltip;
        var EC = protractor.ExpectedConditions;
        return {
            /**
             * set Chart Elements
             * @method setChartElements
             * @param {object} sectionElement
             */
            setChartElements: function (sectionElement) {
                section = sectionElement;
                container = section.element(by.css("div.highcharts-container"));
                svg = container.element(by.css("svg"));
                logger.info("chart elements set for section " + sectionElement)
            },
            /**
             * set svg element
             * @method setSVG
             * @param {object} svgElement
             */
            setSVG: function (svgElement) {
                svg = svgElement;
                logger.info("svg element set " + svgElement)
            },
            /**
             * get svg element
             * @method getSVG
             * @return {object} svg
             */
            getSVG: function () {
                return svg
            },
            /**
             * set container element
             * @method setContianer
             * @param {object} containerElement
             */
            setContainer: function (containerElement) {
                container = containerElement;
                logger.info("container set " + containerElement)
            },
            /**
             * get container element
             * @method getContainer
             * @return {object} container
             */
            getContainer: function () {
                return container
            },
            /**
             * check if chart is displayed
             * @method isChartDisplayed
             * @return {boolean}
             */
            isChartDisplayed: function () {
                return svg.isPresent()
            },

            /**
             * get text displayed in x-axis or y-axis
             * @method getAxisText
             * @param {int} index
             * @return {string}
             */
            getAxisText: function (index) {
                logger.info("get axis text using index " + index);
                return this.getAxisTextByIndex(index)
            },

            /**
             * get text displayed in x-axis
             * @method getXaxisText
             * @return {string}
             */
            getXaxisText: function () {
                logger.info("get x-axis text");
                return this.getAxisTextByIndex(1)
            },

            /**
             * get text displayed in y-axis
             * @method getYaxisText
             * @return {string}
             */
            getYaxisText: function () {
                logger.info("get y-axis text");
                return this.getAxisTextByIndex(2)
            },

            /**
             * get text displayed in axis
             * @method getAxisTextByIndex
             * @param {int} index (1 for x-axis, 2 for y-axis)
             * @return {string}
             */
            getAxisTextByIndex: function (index) {
                logger.info("get axis text by index " + index);
                var deferred = protractor.promise.defer();
                index = index - 1;
                this.waitForChartElement("svg g.highcharts-axis");
                svg = section.element(by.css("svg"));
                svg.all(by.css("g.highcharts-axis")).filter(function (li, id) {
                    return id === index
                }).then(function (texts) {
                    if (texts.length === 1) {
                        texts[0].getText().then(function (text) {
                            deferred.fulfill(text)
                        })
                    } else {
                        deferred.reject(new Error("axis text not found for given index!"));
                        logger.info("axis text not found for given index!")
                    }
                });
                return deferred.promise
            },

            /**
             * get labels displayed in x-axis
             * @method getXaxisLabels
             * @return {list}
             */
            getXaxisLabels: function () {
                logger.info("get x-axis labels");
                return this.getAxisLabels("x")
            },
            /**
             * get labels displayed in y-axis
             * @method getYaxisLabels
             * @return {list}
             */
            getYaxisLabels: function () {
                logger.info("get y-axis labels");
                return this.getAxisLabels("y")
            },
            /**
             * get labels displayed in x-axis or y-axis
             * @param axis
             * @method getAxisLabels
             * @return {list}
             */
            getAxisLabels: function (axis, index) {
                logger.info("get axis labels");
                var deferred = protractor.promise.defer();

                function querytext(x, y) {
                    return new Promise(function (resolve, reject) {
                        resolve(robot.moveMouse(x, y))
                    })
                }

                if (index === undefined)index = 0; else index = index - 1;
                var txtList = svg.all(by.css("svg g.highcharts-" + axis + "axis-labels")).get(index);
                txtList.all(by.css("text")).then(function (list) {
                    var items = new Array;
                    list.forEach(function (li, index) {
                        li.getText().then(function (text) {
                            if (text.trim().length > 0) {
                                items.push(text)
                            }
                        })
                    });
                    return items
                }).then(function (items) {
                    if (items.length > 0)deferred.fulfill(items); else {
                        deferred.reject(new Error(axis + "-axis labels list is empty!"));
                        logger.info(axis + "-axis labels list is empty!")
                    }
                });
                return deferred.promise
            },
            /**
             * get legends displayed in section (outside svg element)
             * @method getLegendsFromSection
             * @return {list}
             */
            getLegendsFromSection: function () {
                logger.info("get legends from section div");
                var deferred = protractor.promise.defer();
                this.waitForChartElement("div.highcharts-legend-item");
                section.all(by.css("div.highcharts-legend-item")).then(function (list) {
                    var items = new Array;
                    list.forEach(function (item, index) {
                        item.getText().then(function (text) {
                            items.push(text)
                        })
                    });
                    return items
                }).then(function (legends) {
                    if (legends.length > 0)deferred.fulfill(legends); else deferred.reject("No legends!")
                });
                return deferred.promise
            },

            /** get highchart data in highchart demo website based on the tooltip
             * @method getChartData
             * @parameter index_data if set to null it will return the whole data for the highcharts
             *                       if it is not null, it will check whehther the tooltipdata exit in the highchart,
             *                                          the mouse will stay on that point if fount it.
             * @return the whole data string and also save the data in csv file.
             * */
            getChartData: function (index_data) {
                var deferred = protractor.promise.defer();
                if (process.env.highchart === "true") {
                    logger.info("get data from section svg");
                    robot.setMouseDelay(0);
                    function querytext(x, y) {
                        return new Promise(function (resolve, reject) {
                            resolve(robot.moveMouse(x, y))
                        })
                    }

                    return TestHelperPO.isElementPresent(element(by.xpath('//*[@id="menu-second"]/ul/li[1]/a'))).then(function () {
                        var mouse = robot.getMousePos();
                        console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y);
                        var screenSize = robot.getScreenSize();
                        var width = screenSize.width;
                        var text_results = new Array;
                        var items = new Array;
                        var ind = index_data;

                        function querylist() {
                            return new Promise(function (resolve, reject) {
                                resolve(svg.all(by.css("g.highcharts-tooltip")).then(function (list) {
                                    if (typeof list != "undefined" && list) {
                                        console.log("list: " + list);
                                        console.log("list length: " + list.length);
                                        list[0].getText().then(function (text) {
                                            if (typeof text != "undefined" && text) {
                                                console.log("in get tooltip 4");
                                                list[0].getText().then(function (text) {
                                                    console.log("in get tooltip 5" + text);
                                                    var result = text.replace(/,\s/g, " ");
                                                    result = result.replace(/[^\w\s("|,|:|(.\d)|-]/g, "");
                                                    items.push(result);
                                                    console.log("text is" + text);
                                                    console.log("index_data is" + ind);
                                                    if (result.toString() === ind.toString()) {
                                                        browser.sleep(3e3).then(function () {
                                                            deferred.fulfill(true);
                                                            return deferred.promise
                                                        })
                                                    }
                                                }).catch(function (error) {
                                                    console.log("error in querrylist 1:" + error)
                                                })
                                            } else {
                                                throw new Error("oh no! text is null")
                                            }
                                        })
                                    } else {
                                        throw new Error("oh no! list is null")
                                    }
                                }))
                            })
                        }

                        console.log("w: " + width);
                        var step = function (i, done) {
                            console.log("I start is: " + i);
                            if (i <= width) {
                                console.log("i is: " + i + "width is: " + width);
                                querytext(i, 350).then(function () {
                                    querylist().then(function (text) {
                                        if (typeof text != "undefined" && text) {
                                            console.log("text is:" + text);
                                            console.log("I in queryList: " + i)
                                        }
                                        step(i + 1)
                                    }).catch(function (error) {
                                        console.log("error in svg all:" + error);
                                        step(i + 1)
                                    })
                                })
                            } else {
                                console.log("should not print");
                                var uniqueArray = items.filter(function (elem, pos) {
                                    return items.indexOf(elem) == pos
                                });
                                var datetime = new Date;
                                var res_string = uniqueArray.join();
                                res_string = res_string.replace(/,/g, "\n");
                                fs.writeFile("./testchartdata" + datetime + ".csv", res_string, function (err) {
                                    if (err)console.log(err); else {
                                        deferred.fulfill(res_string)
                                    }
                                })
                            }
                            console.log("Test")
                        };
                        step(330);
                        console.log("post for loop");
                        return deferred.promise
                    })
                }
                else {
                    logger.info("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    console.log("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    throw new Error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'")
                }
            },

            /** get apm chart data based on the tooltip
             * @method getChartData
             * @parameter   startx the start x position for mouse to start
             *              starty the start y position for mouse to start
             *              endx the end x position will be screenwidth-endx
             *              index_data if set to null it will return the whole data for the highcharts
             *                       if it is not null, it will check whehther the tooltipdata exit in the highchart,
             *                                          the mouse will stay on that point if fount it
             *              rollrange the scroll range for page
             *              rolldirection the scroll direction for page
             * @return the whole data string and also save the data in csv file.
             * */
            getApmChartData: function (startx, starty, endx, index_data, rollrange, rolldirection) {
                var deferred = protractor.promise.defer();
                console.log("Here is the value for highchart: "+process.env.highchart);
                if (process.env.highchart === "true") {
                    var robot = require("robotjs");
                    logger.info("get data from section svg");
                    robot.setMouseDelay(0);
                    function querytext(x, y) {
                        return new Promise(function (resolve, reject) {
                            resolve(robot.moveMouse(x, y))
                        })
                    }

                    return TestHelperPO.isElementPresent(element(by.xpath('//*[@id="highcharts-0"]'))).then(function () {
                        var mouse = robot.getMousePos();
                        var screenSize = robot.getScreenSize();
                        var width = screenSize.width;
                        var text_results = new Array;
                        var items = new Array;
                        var tooltip = container.element(by.css("div.highcharts-tooltip"));

                        function querylist() {
                            return new Promise(function (resolve, reject) {
                                resolve(tooltip.all(by.css("span")).then(function (list) {
                                    if (typeof list != "undefined" && list) {
                                        list[0].getText().then(function (text) {
                                            if (typeof text != "undefined" && text) {
                                                list[0].getText().then(function (text) {
                                                    var editdata = text.split("\n");
                                                    var editdata_1 = editdata.join();
                                                    editdata = editdata_1 + "\n";
                                                    items.push(editdata);
                                                    if (editdata_1.toString() === index_data.toString()) {
                                                        browser.sleep(3e3).then(function () {
                                                            items = [];
                                                            items.push(true)
                                                        })
                                                    } else {
                                                        var int_1 = string_1.replace(/\.[0-9]\d*/g, " ");
                                                        var int_2 = string_2.replace(/\.[0-9]\d*/g, " ");
                                                        int_1 = int_1.toString();
                                                        int_2 = int_2.toString();
                                                        if (int_1 != null && int_2 != null && int_1.valueOf() === int_2.valueOf()) {
                                                            browser.sleep(3e3).then(function () {
                                                                items = [];
                                                                items.push(true)
                                                            })
                                                        }
                                                    }
                                                })
                                            }
                                        })
                                    }
                                }))
                            })
                        }

                        console.log("w: " + width);
                        querytext(startx, starty).then(function () {
                            robot.mouseClick();
                            if(rollrange != null && rolldirection != null)
                            robot.scrollMouse(rollrange, rolldirection)
                        });
                        var step = function (i, done) {
                            if (i <= width - endx) {
                                querytext(i, starty).then(function () {
                                    querylist().then(function () {
                                        if (typeof items[0] === "boolean") {
                                            deferred.fulfill(items[0]);
                                            return deferred.promise;
                                            i = width
                                        } else {
                                            step(i + 1)
                                        }
                                    }).catch(function (error) {
                                        step(i + 1)
                                    })
                                })
                            } else {
                                var uniqueArray = items.filter(function (elem, pos) {
                                    return items.indexOf(elem) == pos
                                });
                                var result = uniqueArray.join(" ");
                                fs.writeFile("./testcharhhihiiktdata.csv", result, function (err) {
                                    if (err)console.log(err); else {
                                        deferred.fulfill(result)
                                    }
                                })
                            }
                        };
                        step(startx);
                        return deferred.promise
                    })
                }
                else {
                    logger.info("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    console.error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    throw new Error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'")
                }
            },
            /** get BM chart data based on the tooltip
             * @method getBMChartData
             * @parameter   startx the start x position for mouse to start
             *              starty the start y position for mouse to start
             *              endx the end x position will be screenwidth-endx
             *              index_data if set to null it will return the whole data for the highcharts
             *                       if it is not null, it will check whehther the tooltipdata exit in the highchart,
             *                                          the mouse will stay on that point if fount it
             *              rollrange the scroll range for page
             *              rolldirection the scroll direction for page
             * @return the whole data string and also save the data in csv file.
             * */
            getBMChartData: function (startx, starty, endx, index_data, rollrange, rolldirection) {
                var deferred = protractor.promise.defer();
                console.log("Here is the value for highchart: " + process.env.highchart);
                if (process.env.highchart === "true") {
                    var robot = require("robotjs");
                    logger.info("get data from section svg");
                    robot.setMouseDelay(0);
                    function querytext(x, y) {
                        return new Promise(function (resolve, reject) {
                            console.log("moving the mouse")
                            resolve(robot.moveMouse(x, y))
                        })
                    }
                    // console.log("I need to get High Chart : ");
                    return TestHelperPO.isElementPresent(element(by.css('#container > div.highcharts-container'))).then(function () {
                        var mouse = robot.getMousePos();
                        var screenSize = robot.getScreenSize();
                        var width = screenSize.width;
                        console.log("width is "+width)
                        var text_results = new Array;
                        var items = new Array;
                        var tooltip = container.element(by.css("g.highcharts-tooltip"));

                        function querylist() {
                            return new Promise(function (resolve, reject) {
                                resolve(tooltip.all(by.css("tspan")).then(function (list) {
                                    //console.log("list is" +list)
                                    if (typeof list != "undefined" && list) {
                                        tooltip.getText().then(function (text) {
                                            if (typeof text != "undefined" && text) {
                                                //list.getText().then(function (text) {
                                                var editdata = text.split("\n");
                                                var editdata_1 = editdata.join();
                                                editdata = editdata_1 + "\n";
                                                console.log("list length is "+list.length);
                                                editdata = editdata.replace(/\d+/g, '$&,');
                                                console.log("editdata is : "+editdata)
                                                items.push(editdata);
                                                if (editdata_1.toString() === index_data.toString()) {
                                                    browser.sleep(3e3).then(function () {
                                                        items = [];
                                                        items.push(true)
                                                    })
                                                } else {
                                                    var int_1 = string_1.replace(/\.[0-9]\d*/g, " ");
                                                    var int_2 = string_2.replace(/\.[0-9]\d*/g, " ");
                                                    int_1 = int_1.toString();
                                                    int_2 = int_2.toString();
                                                    if (int_1 != null && int_2 != null && int_1.valueOf() === int_2.valueOf()) {
                                                        browser.sleep(3e3).then(function () {
                                                            items = [];
                                                            items.push(true)
                                                        })
                                                    }
                                                }
                                                //})
                                            }
                                        })


                                    }
                                }))
                            })
                        }

                        console.log("w: " + width);
                        querytext(startx, starty).then(function () {
                            robot.mouseClick();
                            console.log("Here is the start")
                            console.log("rollrange is "+rollrange)
                            console.log("rolldirection is " +rolldirection)
                            //robot.scrollMouse(rollrange, rolldirection)
                        });
                        var step = function (i, done) {
                            if (i <= width - endx) {
                                console.log("into step"+ i)
                                querytext(i, starty).then(function () {
                                    querylist().then(function () {
                                        if (typeof items[0] === "boolean") {
                                            deferred.fulfill(items[0]);
                                            return deferred.promise;
                                            i = width
                                        } else {
                                            step(i + 10)
                                        }
                                    }).catch(function (error) {
                                        step(i + 10)
                                    })
                                })
                            } else {
                                var uniqueArray = items.filter(function (elem, pos) {
                                    return items.indexOf(elem) == pos
                                });
                                var result = uniqueArray.join(" ");
                                fs.writeFile("./testcharhhihiiktdata.csv", result, function (err) {
                                    if (err)console.log(err); else {
                                        deferred.fulfill(result)
                                    }
                                })
                            }
                        };
                        step(startx);
                        return deferred.promise
                    })
                } else {
                    logger.info("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    console.error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    throw new Error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'")
                }
            },
            /** get OO chart data based on the tooltip
             * @method getOOChartData
             * @parameter time_stamp, the time you want to get the tooltip data, it can be a single time or a time range
             *                        seperated by comma i.e."Monday,Thursday" which means Monday to Friday
             *            loopTime,   the maximam loop time for user to find the time stamp, if the program does not find
             *                        the time stamp after the maximum loopTimes it will quit.
             *            startx,     the start x pixel value for the mouse to start
             *            starty,     the start y pixel value for the mouse to start
             *            endx,       the mouse will move horizontally, the endx is the pixel value to the right edge of
             *                        the screen
             *            rollrange the scroll range for page
             *            rolldirection the scroll direction for page
             * @return the whole data string and also save the data in csv file.
             * */

            getOOChartData: function (time_stamp, loopTime, startx, starty, endx, rollrange, rolldirection) {
                var deferred = protractor.promise.defer();
                if (process.env.highchart === "true") {
                    logger.info("get data from section svg");
                    time_stamp = time_stamp.split(",");
                    robot.setMouseDelay(0);
                    function querytext(x, y) {
                        return new Promise(function (resolve, reject) {
                            resolve(robot.moveMouse(x, y))
                        })
                    }

                    return TestHelperPO.isElementPresent(element(by.xpath('//*[@id="r2d1"]/div/section'))).then(function () {
                        var mouse = robot.getMousePos();
                        var screenSize = robot.getScreenSize();
                        var width = screenSize.width;
                        var text_results = new Array;
                        var items = new Array;
                        var found = new Array;
                        var loopcount = 0;
                        var tooltip = container.element(by.css("div.highcharts-tooltip"));

                        function querylist() {
                            return new Promise(function (resolve, reject) {
                                resolve(tooltip.all(by.css("span")).then(function (list) {
                                    if (typeof list != "undefined" && list) {
                                        list[0].getText().then(function (text) {
                                            if (typeof text != "undefined" && text) {
                                                list[0].getText().then(function (text) {
                                                    var editdata = text.split("\n");
                                                    editdata[3] = editdata[3].replace(/,/g, " ");
                                                    var temptagtime = (editdata[2] + editdata[3]).toString();
                                                    editdata = (editdata[0] + " " + editdata[1]).toString() + "," + (editdata[2] + editdata[3]).toString();
                                                    editdata += "\n";
                                                    items.push(editdata);
                                                    if (temptagtime == time_stamp[0] || temptagtime == time_stamp[1] || loopcount === loopTime) {
                                                        found.push(true)
                                                    }
                                                })
                                            }
                                        })
                                    }
                                }))
                            })
                        }

                        querytext(300, 450).then(function () {
                            robot.mouseClick();
                            robot.scrollMouse(rollrange, rolldirection)
                        });
                        var step = function (i, done) {
                            if (i <= width - endx) {
                                querytext(i, starty).then(function () {
                                    querylist().then(function () {
                                        if (found[0] === true) {
                                            var uniqueArray = items.filter(function (elem, pos) {
                                                return items.indexOf(elem) == pos
                                            });
                                            var result = uniqueArray.join(" ");
                                            fs.writeFile("./testOOhighchartdata.csv", result, function (err) {
                                                if (err)console.log(err); else {
                                                    deferred.fulfill(result)
                                                }
                                            })
                                        } else {
                                            step(i + 1)
                                        }
                                    }).catch(function (error) {
                                        step(i + 1)
                                    })
                                })
                            } else {
                                loopcount++;
                                step(startx)
                            }
                        };
                        step(startx);

                    })
                }
                else {
                    logger.info("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    console.error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    throw new Error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                }
            },

            /** get the tooltip information for apmhighchart
             * @method getTooltipInfo
             * @parameter Timestamp,  the timerange you want to get for the tooltip, if set to null will print no data.
             *            Tagname,    the tagname or tagname list  you want to get for highchart, if set to null,
             *                        will print all tag
             *            startx,     the start x pixel value for the mouse to start
             *            starty,     the start y pixel value for the mouse to start
             *            endx,       the mouse will move horizontally, the endx is the pixel value to the right edge of
             *                        the screen
             *            rollrange the scroll range for page
             *            rolldirection the scroll direction for page
             *            needVerify,  if set to true, it will verify the tagname exits in the tooltip,
             *                                        return ture if the tag exit and
             *                                      return false if the tag is not exit
             *                         if set to false, it will return the value under the tag name,
             *                         if the tag does not exit, it will put "no data" under that tag
             */
            getTooltipInfo: function (Timestamp, Tagname, needVerify, startx, starty, endx, rollrange, rolldirection) {
                var deferred = protractor.promise.defer();
                if (process.env.highchart === "true") {
                    logger.info("get data from section svg");
                    console.log("rollrange" + rollrange);
                    console.log("rolldirection" + rolldirection);
                    robot.setMouseDelay(0);
                    function querytext(x, y) {
                        return new Promise(function (resolve, reject) {
                            resolve(robot.moveMouse(x, y))
                        })
                    }

                    function scrollpage(x, y) {
                        return new Promise(function (resolve, reject) {
                            resolve(robot.scrollMouse(x, y))
                        })
                    }

                    scrollpage(rollrange, rolldirection);
                    if (!Timestamp || 0 === Timestamp.length) {
                        Timestamp = " "
                    }
                    if (!Tagname || 0 === Tagname.length) {
                        Tagname = " "
                    }
                    var timevalue = Timestamp.split(",");
                    var tagvaule = Tagname.split(",");
                    var timeindex = 0;
                    return TestHelperPO.isElementPresent(element(by.xpath('//*[@id="highcharts-0"]'))).then(function () {
                        var mouse = robot.getMousePos();
                        var screenSize = robot.getScreenSize();
                        var width = screenSize.width;
                        var text_results = new Array;
                        var items = new Array;
                        var tooltip = container.element(by.css("div.highcharts-tooltip"));

                        function querylist() {
                            return new Promise(function (resolve, reject) {
                                resolve(tooltip.all(by.css("span")).then(function (list) {
                                    if (typeof list != "undefined" && list) {
                                        list[0].getText().then(function (text) {
                                            if (typeof text != "undefined" && text) {
                                                list[0].getText().then(function (text) {
                                                    if (timeindex < 2) {
                                                        var res = new Array;
                                                        var editdata = text.split("\n");
                                                        res.push(editdata[0].toString());
                                                        if (timevalue.length > 1 && timeindex > 0 || editdata[0].toString() === timevalue[timeindex].toString()) {
                                                            browser.sleep(1e3).then(function () {
                                                                var getValue = function (m, j) {
                                                                    if (m < editdata.length && j < tagvaule.length) {
                                                                        var insideloop = function (m) {
                                                                            var temptag = editdata[m].split(":");
                                                                            if (temptag[0].toString() === tagvaule[j].toString()) {
                                                                                res.push(temptag[1].toString());
                                                                                getValue(1, j + 1)
                                                                            } else {
                                                                                if (m === editdata.length - 1) {
                                                                                    if (needVerify === false) {
                                                                                        var missingtag = "no data";
                                                                                        res.push(missingtag);
                                                                                        getValue(1, j + 1)
                                                                                    } else {
                                                                                        items = [];
                                                                                        items.push(false)
                                                                                    }
                                                                                } else {
                                                                                    getValue(m + 1, j)
                                                                                }
                                                                            }
                                                                        };
                                                                        insideloop(m)
                                                                    } else {
                                                                        if (needVerify === false) {
                                                                            items.push(res.join());
                                                                            if (editdata[0].toString() === timevalue[timeindex].toString()) {
                                                                                timeindex++;
                                                                                return items
                                                                            }
                                                                        } else {
                                                                            items = [];
                                                                            items.push(true)
                                                                        }
                                                                    }
                                                                };
                                                                getValue(1, 0)
                                                            })
                                                        }
                                                    }
                                                })
                                            }
                                        })
                                    }
                                }))
                            })
                        }

                        querytext(300, 450).then(function () {
                            robot.mouseClick()
                        });
                        var step = function (i, done) {
                            if (i <= width - endx) {
                                querytext(i, starty).then(function () {
                                    querylist().then(function () {
                                        if (typeof items[0] === "boolean") {
                                            deferred.fulfill(items[0]);
                                            return deferred.promise;
                                            i = width
                                        } else if (needVerify === false && timeindex === timevalue.length) {
                                            var uniqueArray = items.filter(function (elem, pos) {
                                                return items.indexOf(elem) == pos
                                            });
                                            deferred.fulfill(uniqueArray);
                                            return deferred.promise;
                                            i = width
                                        } else {
                                            step(i + 1)
                                        }
                                    }).catch(function (error) {
                                        step(i + 1)
                                    })
                                })
                            } else {
                                var uniqueArray = items.filter(function (elem, pos) {
                                    return items.indexOf(elem) == pos
                                });
                                var result = uniqueArray.join(" ");
                                fs.writeFile("./testcharhhihiiktdata.csv", result, function (err) {
                                    if (err)console.log(err); else {
                                        deferred.fulfill(items)
                                    }
                                })
                            }
                        };
                        step(startx);
                        return deferred.promise
                    })
                }
                else {
                    logger.info("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    console.log("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'");
                    throw new Error("Highchart disabled. Please enable highchart in .env to use functions. Make 'highchart=true'")
                }
            },

            /** compare the data with the data in the file line by line with only the integer part
             * @method compareData
             * @parameter testdata, the string of the data you get
             ordfile, the path for the file that contains data
             * @return boolean value if the similarity is over 95% then true, else false
             */
            compareData: function (testdata, orgfile) {
                var deferred = protractor.promise.defer();
                console.log("type of testdata " + typeof testdata);
                testdata = testdata.toString();
                console.log("type of testdata " + typeof testdata);
                fs.readFile(orgfile, "utf8", function (err, contents) {
                    if (err)throw err;
                    var arr_now = testdata.split("\n");
                    var arr_org = contents.split("\n");
                    var count = 0;
                    for (var i = 0, j = 0; i < arr_now.length && j < arr_org.length;) {
                        var string_1 = arr_now[i].toString().trim();
                        var string_2 = arr_org[j].toString().trim();
                        if (string_1.valueOf() === string_2.valueOf()) {
                            count++;
                            i++;
                            j++;
                            continue
                        } else {
                            var int_1 = string_1.replace(/\.[0-9]\d*/g, " ");
                            var int_2 = string_2.replace(/\.[0-9]\d*/g, " ");
                            int_1 = int_1.toString();
                            int_2 = int_2.toString();
                            if (int_1 != null && int_2 != null && int_1.valueOf() === int_2.valueOf()) {
                                count++;
                                i++;
                                j++;
                                continue
                            } else {
                                console.log("int_1 is " + int_1);
                                console.log("int_2 is " + int_2);
                                i++;
                                j++;
                                continue
                            }
                        }
                    }
                    var res = count / arr_org.length;
                    console.log("res is " + res);
                    console.log("count is " + count);
                    if (res >= .9) {
                        deferred.fulfill(true)
                    } else {
                        deferred.fulfill(false)
                    }
                });
                return deferred.promise
            },
            getLegendsFromSVG: function () {
                logger.info("get legends from section svg");
                var deferred = protractor.promise.defer();
                this.waitForChartElement("svg g.highcharts-legend-item");
                svg.all(by.css("g.highcharts-legend-item")).then(function (list) {
                    var items = new Array;
                    list.forEach(function (item, index) {
                        item.getText().then(function (text) {
                            items.push(text);
                            deferred.fulfill(items)
                        })
                    })
                });
                return deferred.promise
            },

            getParallelAxiesOrder: function () {
                var deferred = protractor.promise.defer();
                logger.info("get parallel axies order from left to right");
                this.waitForChartElement(element.all("#chartSVG > g:nth-child(2) > g").get(1));
                var axises = element.all(by.css("#chartSVG > g:nth-child(2) > g"));
                axises.count().then(function (len) {
                    console.log("axises length is" + len)
                    var i = 0;
                    var res = new Array()
                    var step = function (i, done) {
                        if (i < len - 1) {
                            var curaxis = axises.get(i)
                            curaxis.getAttribute("transform").then(function (translate) {
                                console.log(" I am here")
                                var temp = new Array();
                                translate = translate.toString()
                                console.log("translate is " + translate)
                                var start = translate.indexOf("(")
                                console.log("start is " + start)
                                var end = translate.indexOf(")")
                                console.log("end is " + end)
                                var numbers_string = translate.substring(start + 1, end)
                                var x = Number(numbers_string.split(",")[0])
                                console.log("x is " + x)
                                element.all(by.css('#chartSVG > g:nth-child(2) > g #message')).get(i).getInnerHtml().then(function (text) {
                                    //console.log("text is " + text)
                                    var atext = text.toString();
                                    //console.log("atext is " + atext)
                                    var btext = atext.replace(/<.*>/g, "");
                                    //console.log("btext is " + btext)
                                    temp.push(x)
                                    temp.push(btext)
                                    res.push(temp)
                                    step(i + 1)
                                    //})
                                })

                            })
                            // })
                        } else {
                            var sortedRes = res.sort(function (a, b) {
                                return a[0] - b[0];
                            });
                            deferred.fulfill(sortedRes);
                        }

                    };
                    step(0)

                })
                return deferred.promise
            },

            verifyParellelTimerangecoverall: function(){
                var deferred = protractor.promise.defer();
                logger.info("get parallel axies order from left to right");
                this.waitForChartElement(element.all("#chartSVG > g:nth-child(2) > g").last());
                var axises = element.all(by.css("#chartSVG > g:nth-child(2) > g"));
                var brush = element(by.css("#chartSVG > g:nth-child(2) > g > rect.overlay"));
                var res;
                brush.getAttribute("x").then(function(start){
                    console.log("start "+ start)
                    var select_range = element(by.css("#chartSVG > g:nth-child(2) > g > rect.selection"))
                    select_range.getAttribute("x").then(function(start_position){
                        if(start == start_position){
                            brush.getAttribute("width").then(function(length){
                                console.log("end_position "+ length)
                                var select_range = element(by.css("#chartSVG > g:nth-child(2) > g > rect.selection"))
                                select_range.getAttribute("width").then(function(end_position){
                                    if(length == end_position){
                                        deferred.fulfill(false)
                                    }
                                })
                            })
                        }else{
                            deferred.fulfill(false)
                        }
                    })
                })
                return deferred.promise

            },
            waitForChartElement: function (element) {
                browser.wait(EC.presenceOf(section.element(by.css(element))), 6e4)
            }
        }
    };
    module.exports = new HighChart
})();